<template>
  <div class="container">
    <div class="nav">
      <router-link to="/my">
        <img src="../../assets/icon/common-left.png" alt="" class="back">
      </router-link>
      <p class="title">賬戶充值</p>
      <img src="../../assets/icon/file.png" alt="" class="rili">
    </div>
    <div class="box">
      <div class="start">
        <p style="margin-left: 10px">選擇幣種</p>
        <select name="" id="" v-model="type">
          <option value="btc">BTC账户</option>
          <option value="usdt">USDT账户</option>
          <option value="eth">ETH账户</option>
        </select>
      </div>
      <div class="end">
      </div>
    </div>
    <div class="code">
      <div id="query">
        <div id="qrcode" ref="qrcode"></div>
      </div>
      <div>{{url}}</div>
      <div :data-clipboard-text="url" data-clipboard-action="copy" @click="copyLink()" class="copy-btn">
        <mt-button class="copy" type="primary" >複製地址</mt-button>
      </div>
    </div>
    <div class="num">
      <div class="formNum">
        <input type="text" placeholder="請輸入充值數量">
      </div>
    </div>
    <mt-button type="primary" style="width: 342px;height: 40px;margin:16px 13px 10px 13px;letter-spacing: 1.5px
">確認充值
    </mt-button>
    <div class="ts">
      <div>
        <p><img src="../../assets/icon/coin/USDT.png" alt="" class="notice"></p>
        <p><img src="../../assets/icon/coin/USDT.png" alt="" class="notice"></p>
      </div>
      <div>
        <p>爲了賬戶安全,請先簽約,再進行提幣</p>
        <p>提交提幣申請後,訂單可以在資金流水中進行查看審核進度24小時内完成審核進度，審核通過后資產
          將在整個網絡節點確認後到達提現賬戶</p>
      </div>


    </div>
  </div>
</template>

<script>
  import {Button, MessageBox} from 'mint-ui';
  import clipboard from 'clipboard';
  import QRCode from 'qrcodejs2'
  import {rechargeUrl} from "../../api/rechargeUrl";
  //注册到vue原型上
  Vue.prototype.clipboard = clipboard;
  import Vue from 'vue';

  Vue.component(Button.name, Button);
  export default {
    name: "index",
      data(){
        return{
            url:'',
            type:'btc',
            userInfo:{}
        }
      },

      methods:{
        //复制功能
          copyLink() {
              let _this = this;
              let clipboard = new this.clipboard(".copy-btn");
              clipboard.on('success', function () {
                  _this.$toast("复制成功")
              });
              clipboard.on('error', function () {
                  _this.$toast("复制失败")
              });
          },
          //二维码生成
          qrcode () {
              let qrcode = new QRCode('qrcode', {
                  width: 120, // 设置宽度，单位像素
                  height: 120, // 设置高度，单位像素
                  text: this.url   // 设置二维码内容或跳转地址
              })
          },
          closeCode () {
              this.$refs.qrcode.innerHTML = '';
          }

      },
      mounted() {
        if (this.url!=''){
            this.$nextTick(() => {
                this.qrcode()
            })
        }

      },
      created(){
          this.userInfo=JSON.parse(window.localStorage.getItem('userInfo'));
          rechargeUrl({
              uid:this.userInfo.uid,
              currency:this.type
          }).then((data)=>{
              if (data.errorCode==0){
                  if (data.data[this.type+'_address']==''){
                      this.url='暂无地址';
                  }else {
                      this.url=data.data[this.type+'_address'];
                      console.log(this.url)
                  }
              }else {
                  MessageBox.alert(data.msg)
              }

          });
      },

      watch:{
         url:{
           handler(newVal){
              this.closeCode();
                   this.$nextTick(() => {
                       this.qrcode()
                   });

               },
             deep: true
         },
          type: {
              handler(newVal) {
                  rechargeUrl({
                      uid:this.userInfo.uid,
                      currency:newVal
                  }).then((data)=>{
                      if (data.errorCode==0){
                          if (data.data[newVal+'_address']==''){
                              this.url='暂无地址';
                          }else {
                              this.url=data.data[newVal+'_address'];
                              console.log(this.url)
                          }

                      }else {
                          MessageBox.alert(data.msg)
                      }

                  })
              },
              deep: true
          }

      }
  }
</script>

<style scoped lang="scss">
  .container {
    width: 100%;
  }
  .copy{
    font-size: 12px;
    width:82px;
    height: 30px;
    letter-spacing: 1.5px;
  }

  .nav {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    height: 40px;
    font-size: 17px;
    font-weight: bold;

    .back {
      margin-left: 17.5px;
      width: 15px;
      height: 15px;
    }

    .rili {
      margin-right: 17.5px;
      width: 20px;
      height: 20px;
    }
  }

  .box {
    border-radius: 5px;
    margin: 10px auto;
    width: 355.2px;
    height: 80px;
    display: flex;
    flex-direction: column;
    align-items: center;

    p {
      font-size: 13px;
    }

    .start {
      display: flex;
      flex-direction: row;
      width: 100%;
      height: 50px;
      align-items: center;

      select {
        margin-left: 30px;
        width: 200px;
        border: none;
        height: 50px;
        font-size: 15px;
        font-weight: bold;
      }
    }

    .end {
      width: 96%;
      height: 2px;
      background-color: #26a2ff;
      margin-top: 6px;
    }
  }

  .num {

    width: 355.2px;
    height: 42px;
    margin: 0 auto;

    .formNum {
      width: 268.2px;
      height: 34px;
      display: flex;
      flex-direction: row;
      align-items: center;
      margin: 0 auto;

      input {
        border-radius: 5px;
        padding: 5px 5px 5px 10px;
        border: none;
        border-bottom: 1px solid #26a2ff;
        width: 268.2px;
        height: 34px;
        font-size: 14px;
        margin-left: 6px;
      }

    }

  }
  .code{
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 220px;
    font-size: 14px;
    img {
      width: 120px;
      height:120px;
    }
    div{
      margin-top: 10px
    ;
    }
  }
  .have {
    font-size: 12px;
    margin-left: 9px;
    margin-top: -7px;

    p {
      margin-top: 2px;
    }
  }

  .ts {
    margin-left: 4%;
    display: flex;
    flex-direction: row;
    width: 330px;
    height: 45px;
    font-size: 14px;

    img {
      margin-top: 2px;
      margin-right: 2px;
      height: 14px;
      width: 14px;
    }

  }
</style>
